package routers

import (
	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
	"net/http"
	"vtrip/global"
	"vtrip/internal/middleware"

	_ "vtrip/docs"
	"vtrip/internal/routers/api"
)

func NewRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	// 解决跨域问题中间件
	r.Use(middleware.Cors())

	// 使用接口校验中间件
	r.Use(middleware.Translations())

	// 获取接口文档
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 微信登录
	user := api.NewUser()
	r.POST("/login/wchat", user.WChatLogin)

	// 公开静态资源
	r.StaticFS("/static", http.Dir(global.AppSetting.UploadSavePath))

	// 需要 token 验证的接口
	advanceGroup := r.Group("/")
	advanceGroup.Use(middleware.JWT())

	// 上传文件
	upload := api.NewUpload()
	advanceGroup.POST("/upload/file", upload.UploadFile)

	// 文章路由组
	article := api.NewArticle()
	articleGroup := advanceGroup.Group("/article")
	{
		articleGroup.POST("/all", article.All)
		articleGroup.POST("/get", article.Get)
		articleGroup.POST("/publish", article.Publish)
		articleGroup.POST("/like", article.Like)
		articleGroup.POST("/dislike", article.Dislike)

		// 文章评论路由组
		articleComment := api.NewArticleComment()
		articleCommentGroup := articleGroup.Group("/comment")
		{
			articleCommentGroup.POST("/all", articleComment.All)
			articleCommentGroup.POST("/get", articleComment.Get)
			articleCommentGroup.POST("/make", articleComment.Make)
			articleCommentGroup.POST("/reply", articleComment.Reply)
			articleCommentGroup.POST("/like", articleComment.Like)
			articleCommentGroup.POST("/dislike", articleComment.Dislike)
		}
	}

	// 旅游行程路由组
	itinerary := api.NewItinerary()
	itineraryGroup := advanceGroup.Group("/itinerary")
	{
		itineraryGroup.POST("/all", itinerary.All)
		itineraryGroup.POST("/get", itinerary.Get)
		itineraryGroup.POST("/publish", itinerary.Publish)
	}

	// 景点信息路由组
	scenery := api.NewScenery()
	sceneryGroup := advanceGroup.Group("/scenery")
	{
		sceneryGroup.POST("/all", scenery.All)
		sceneryGroup.POST("/get", scenery.Get)
		sceneryGroup.POST("/create", scenery.Create)
	}

	tourismGroup := advanceGroup.Group("/tourism")
	{
		// 旅游项目路由组
		tourismProject := api.NewTourismProject()
		tourismProjectGroup := tourismGroup.Group("/project")
		{
			tourismProjectGroup.POST("/all", tourismProject.All)
			tourismProjectGroup.POST("/add", tourismProject.Add)
		}

		// 旅游类型路由组
		tourismType := api.NewTourismType()
		tourismTypeGroup := tourismGroup.Group("/type")
		{
			tourismTypeGroup.POST("/all", tourismType.All)
			tourismTypeGroup.POST("/add", tourismType.Add)
		}
	}

	return r
}
